package org.lee.domain.service.logic;

import org.lee.domain.model.vo.TreeNodeLink;

import java.util.List;
import java.util.Map;

/**
 * BaseLogicFilter
 * 基础决策
 * @author dralee
 * @date 2021-11-22 14:47:20
 **/
public abstract class BaseLogicFilter implements ILogicFilter{

    @Override
    public long filter(String matterValue, List<TreeNodeLink> treeNodeLinkList) {
        for(TreeNodeLink link : treeNodeLinkList){
            if(decisionLogic(matterValue, link)){
                return link.getNodeIdTo();
            }
        }
        return 0;
    }

    private boolean decisionLogic(String matterValue, TreeNodeLink link) {
        switch (link.getRuleLimitType()){
            case EQUALS:
                return matterValue.equals(link.getRuleLimitValue());
            case GT:
                return Double.parseDouble(matterValue) > Double.parseDouble(link.getRuleLimitValue());
            case LT:
                return Double.parseDouble(matterValue) < Double.parseDouble(link.getRuleLimitValue());
            case GTE:
                return Double.parseDouble(matterValue) >= Double.parseDouble(link.getRuleLimitValue());
            case LTE:
                return Double.parseDouble(matterValue) <= Double.parseDouble(link.getRuleLimitValue());
            default:
                return false;
        }
    }

    @Override
    public abstract String matterValue(long treeId, String userId, Map<String, String> decisionMatter);
}
